<%@ page language="java" import="com.alibaba.fastjson.JSONArray" pageEncoding="UTF-8" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.common.DateUtil" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%--在线考试交卷--%>
<%
    JSONObject jsonData = new JSONObject();
    RecordSet rs = new RecordSet();
    BaseBean baseBean = new BaseBean();
    String formtable = "formtable_main_286";//在线考试答题
    String requestid = Util.null2String(request.getParameter("requestid"));
    baseBean.writeLog(">>>>>getsubmitTest_Ajax>>>requestid="+requestid);
    updateDtsc(requestid, formtable);
    String mainid = getFieldValByname("id", requestid, formtable);
    baseBean.writeLog(">>>>>getsubmitTest_Ajax>>>mainid="+mainid);
    String result = Util.null2String(request.getParameter("result"));
    baseBean.writeLog(">>>>>getsubmitTest_Ajax>>>result="+result);
    baseBean.writeLog("result="+result.length());
    if (result.length()<=2) {
        baseBean.writeLog(">>>>>getsubmitTest_Ajax>>>点击保存按钮后进入代码");
        //单选题
        rs.executeQuery("select id,tm,fz,txnr from " + formtable + "_dt1 where mainid=?", mainid);
        while (rs.next()) {
            int fzs = 0;
            String id = Util.null2String(rs.getString("id"));
            String tm = Util.null2String(rs.getString("tm"));
            int fz = Util.getIntValue(rs.getString("fz"));
            String txnr = Util.null2String(rs.getString("txnr"));
            JSONObject zqdajson = getRightAnswer(tm);
            baseBean.writeLog(">>>>>getsubmitTest_Ajax>>>单选题>>>>zqdajson="+zqdajson);
            JSONArray zqdaList = JSONArray.parseArray(zqdajson.getString("zqda"));
            for (int j = 0; j < zqdaList.size(); j++) {
                String xxid = zqdaList.getJSONObject(j).getString("xxid");
                baseBean.writeLog(">>>>>getsubmitTest_Ajax>>>单选题>>>>xxid="+xxid+",txnr="+txnr);
                fzs += txnr.equals(xxid) ? fz : 0;
            }
            baseBean.writeLog(">>>>>getsubmitTest_Ajax>>>单选题>>>>fzs="+fzs);
            rs.executeUpdate("update " + formtable + "_dt1 set df='" + fzs + "' where mainid=? and id=?", mainid, id);
        }
        //多选题
        rs.executeQuery("select id,tm,fz,txnr from " + formtable + "_dt2 where mainid=? ", mainid);
        while (rs.next()) {
            int fzs = 0;
            String id = Util.null2String(rs.getString("id"));
            String tm = Util.null2String(rs.getString("tm"));
            int fz = Util.getIntValue(rs.getString("fz"));
            JSONObject zqdajson = getRightAnswer(tm);
            String txnr = Util.null2String(rs.getString("txnr"));
            JSONArray zqdaList = JSONArray.parseArray(zqdajson.getString("zqda"));
            baseBean.writeLog(">>>>>getsubmitTest_Ajax>>>多选题>>>>zqdajson="+zqdajson);
            String xxids = "";
            for (int j = 0; j < zqdaList.size(); j++) {
                String xxid = zqdaList.getJSONObject(j).getString("xxid");
                xxids += "".equals(xxids) ? xxid : "," + xxid;
                baseBean.writeLog(">>>>>getsubmitTest_Ajax>>>多选题>>>>xxids="+xxids+",txnr="+txnr);
            }
            fzs += txnr.equals(xxids) ? fz : 0;
            baseBean.writeLog(">>>>>getsubmitTest_Ajax>>>多选题>>>>fzs="+fzs+",mxid="+id);
            rs.executeUpdate("update " + formtable + "_dt2 set df='" + fzs + "' where mainid=? and id=?", mainid, id);
        }
        //判断题
        rs.executeQuery("select id,tm,fz,txnr from " + formtable + "_dt3 where mainid=?", mainid);
        while (rs.next()) {
            int fzs = 0;
            String id = Util.null2String(rs.getString("id"));
            String tm = Util.null2String(rs.getString("tm"));
            int fz = Util.getIntValue(rs.getString("fz"));
            String txnr = Util.null2String(rs.getString("txnr"));
            JSONObject zqdajson = getRightAnswer(tm);
            JSONArray zqdaList = JSONArray.parseArray(zqdajson.getString("zqda"));
            baseBean.writeLog(">>>>>getsubmitTest_Ajax>>>判断题>>>>zqdajson="+zqdajson);
            for (int j = 0; j < zqdaList.size(); j++) {
                String xxid = zqdaList.getJSONObject(j).getString("xxid");
                fzs += txnr.equals(xxid) ? fz : 0;
                baseBean.writeLog(">>>>>getsubmitTest_Ajax>>>判断题>>>>xxid="+xxid+",txnr="+txnr);
            }
            baseBean.writeLog(">>>>>getsubmitTest_Ajax>>>判断题>>>>fzs="+fzs+",mxid="+id);
            rs.executeUpdate("update " + formtable + "_dt3 set df='" + fzs + "' where mainid=? and id=?", mainid, id);
        }
    } else {
        baseBean.writeLog(">>>>>getsubmitTest_Ajax>>>未点击保存按钮后进入代码");
        JSONObject resultJSON = JSONObject.parseObject(result);
        baseBean.writeLog(">>>>>getsubmitTest_Ajax>>>resultJSON="+resultJSON);
        //单选题
        JSONArray dxtList = JSONArray.parseArray(resultJSON.getString("dxt"));
        if (dxtList.size() > 0) {
            for (int i = 0; i < dxtList.size(); i++) {
                int xh = Util.getIntValue(dxtList.getJSONObject(i).getString("xh").substring(2)) + 1;
                String txnr = dxtList.getJSONObject(i).getString("txnr");
                baseBean.writeLog(">>>>>getsubmitTest_Ajax>>>单选题>>>>xh="+xh+",txnr="+txnr);
                rs.executeQuery("select tm,fz from " + formtable + "_dt1 where mainid=? and px=?", mainid, xh);
                int fzs = 0;
                if (rs.next()) {
                    String tm = Util.null2String(rs.getString("tm"));
                    int fz = Util.getIntValue(rs.getString("fz"));
                    JSONObject zqdajson = getRightAnswer(tm);
                    JSONArray zqdaList = JSONArray.parseArray(zqdajson.getString("zqda"));
                    baseBean.writeLog(">>>>>getsubmitTest_Ajax>>>单选题>>>>zqdajson="+zqdajson);
                    for (int j = 0; j < zqdaList.size(); j++) {
                        String xxid = zqdaList.getJSONObject(j).getString("xxid");
                        fzs += txnr.equals(xxid) ? fz : 0;
                        baseBean.writeLog(">>>>>getsubmitTest_Ajax>>>单选题>>>>xxid="+xxid+",txnr="+txnr);
                    }
                    baseBean.writeLog(">>>>>getsubmitTest_Ajax>>>单选题>>>>fzs="+fzs+",xh="+xh);
                    rs.executeUpdate("update " + formtable + "_dt1 set txnr='" + txnr + "',df='" + fzs + "' where mainid=? and px=?", mainid, xh);
                }
            }
        }
        //多选题
        JSONArray duoxtList = JSONArray.parseArray(resultJSON.getString("duoxt"));
        if (duoxtList.size() > 0) {
            for (int i = 0; i < duoxtList.size(); i++) {
                int xh = Util.getIntValue(duoxtList.getJSONObject(i).getString("xh").substring(2)) + 1;
                String txnr = duoxtList.getJSONObject(i).getString("txnr");
                baseBean.writeLog(">>>>>getsubmitTest_Ajax>>>多选题>>>>xh="+xh+",txnr="+txnr);
                rs.executeQuery("select tm,fz from " + formtable + "_dt2 where mainid=? and px=?", mainid, xh);
                int fzs = 0;
                if (rs.next()) {
                    String tm = Util.null2String(rs.getString("tm"));
                    int fz = Util.getIntValue(rs.getString("fz"));
                    JSONObject zqdajson = getRightAnswer(tm);
                    baseBean.writeLog(">>>>>getsubmitTest_Ajax>>>多选题>>>>zqdajson="+zqdajson);
                    JSONArray zqdaList = JSONArray.parseArray(zqdajson.getString("zqda"));
                    String xxids = "";
                    for (int j = 0; j < zqdaList.size(); j++) {
                        String xxid = zqdaList.getJSONObject(j).getString("xxid");
                        xxids += "".equals(xxids) ? xxid : "," + xxid;
                        baseBean.writeLog(">>>>>getsubmitTest_Ajax>>>多选题>>>>xxids="+xxids+",txnr="+txnr);
                    }
                    fzs += txnr.equals(xxids) ? fz : 0;
                    baseBean.writeLog(">>>>>getsubmitTest_Ajax>>>多选题>>>>fzs="+fzs+",xh="+xh);
                    rs.executeUpdate("update " + formtable + "_dt2 set txnr='" + txnr + "',df='" + fzs + "' where mainid=? and px=?", mainid, xh);
                }
            }
        }
        //判断题
        JSONArray pdtList = JSONArray.parseArray(resultJSON.getString("pdt"));
        if (pdtList.size() > 0) {
            for (int i = 0; i < pdtList.size(); i++) {
                int xh = Util.getIntValue(pdtList.getJSONObject(i).getString("xh").substring(2)) + 1;
                String txnr = pdtList.getJSONObject(i).getString("txnr");
                baseBean.writeLog(">>>>>getsubmitTest_Ajax>>>判断题>>>>xh="+xh+",txnr="+txnr);
                rs.executeQuery("select tm,fz from " + formtable + "_dt3 where mainid=? and px=?", mainid, xh);
                int fzs = 0;
                if (rs.next()) {
                    String tm = Util.null2String(rs.getString("tm"));
                    int fz = Util.getIntValue(rs.getString("fz"));
                    JSONObject zqdajson = getRightAnswer(tm);
                    baseBean.writeLog(">>>>>getsubmitTest_Ajax>>>判断题>>>>zqdajson="+zqdajson);
                    JSONArray zqdaList = JSONArray.parseArray(zqdajson.getString("zqda"));
                    for (int j = 0; j < zqdaList.size(); j++) {
                        String xxid = zqdaList.getJSONObject(j).getString("xxid");
                        fzs += txnr.equals(xxid) ? fz : 0;
                        baseBean.writeLog(">>>>>getsubmitTest_Ajax>>>判断题>>>>xxid="+xxid+",txnr="+txnr);
                    }
                    baseBean.writeLog(">>>>>getsubmitTest_Ajax>>>判断题>>>>fzs="+fzs+",xh="+xh);
                    rs.executeUpdate("update " + formtable + "_dt3 set txnr='" + txnr + "',df='" + fzs + "' where mainid=? and px=?", mainid, xh);
                }
            }
        }
        //简答题
        JSONArray jdtList = JSONArray.parseArray(resultJSON.getString("jdt"));
        if (jdtList.size() > 0) {
            for (int i = 0; i < jdtList.size(); i++) {
                int xh = Util.getIntValue(jdtList.getJSONObject(i).getString("xh").substring(2)) + 1;
                String txnr = jdtList.getJSONObject(i).getString("txnr");
                baseBean.writeLog(">>>>>getsubmitTest_Ajax>>>简答题>>>>xh="+xh+",txnr="+txnr);
                rs.executeUpdate("update " + formtable + "_dt4 set txnr='" + txnr + "' where mainid=? and px=?", mainid, xh);
            }
        }

    }

    jsonData.put("status", true);
    out.print(jsonData.toJSONString());
%>

<%!
    /**
     * 获取表单字段
     */
    public String getFieldValByname(String filename, String requestid, String formtable) {
        RecordSet rs = new RecordSet();
        rs.executeQuery("select " + filename + " from " + formtable + " where requestid=?", requestid);
        rs.next();
        return Util.null2String(rs.getString(1));
    }

    /**
     * 获取考试考题正确答案
     */
    public JSONObject getRightAnswer(String id) {
        RecordSet rs = new RecordSet();
        JSONObject jsonData = new JSONObject();
        JSONArray childList = new JSONArray();
        rs.executeQuery("select xx,zqda,ROW_NUMBER() over(order by px,id) px from uf_ktsz_dt1 where mainid=? order by px,id", id);
        while (rs.next()) {
            String zqda = Util.null2String(rs.getString("zqda"));
            if("0".equals(zqda)){
                JSONObject json = new JSONObject();
                json.put("xxid", Util.null2String(rs.getString("px")));
                json.put("xxnr", Util.null2String(rs.getString("xx")));
                childList.add(json);
            }
        }
        jsonData.put("zqda", childList);
        return jsonData;
    }

    /**
     * 更新答题时长
     */
    public static void updateDtsc(String requestid, String formtable) {
        RecordSet rs = new RecordSet();
        String fullDate = DateUtil.getFullDate();
        rs.executeQuery("select ksdjsj,isnull(sfbc,1) sfbc from "+formtable+" where requestid=?",requestid);
        rs.next();
        String ksdjsj = Util.null2String(rs.getString("ksdjsj"));
        String sfbc = Util.null2String(rs.getString("sfbc"));
        String sql = "";
        if ("1".equals(sfbc)) {
            long timeInterval = DateUtil.timeInterval(ksdjsj, fullDate);
            String dtsc = secondConvertHourMinSecond(timeInterval);
            sql = "update " + formtable + " set kszt=2,djjssjfz='" + fullDate + "',dtsc='" + dtsc + "' where requestid=?";
            rs.executeUpdate(sql, requestid);
            sql = "update uf_kstjtz_dt1 set kszt=2,sjtjsj='" + fullDate + "',dtscfz='" + dtsc + "' where sjxq=?";
            rs.executeUpdate(sql, requestid);
        }
    }
    /**
     * 将秒转为时分秒格式【01:01:01】
     */
    public static String secondConvertHourMinSecond(Long second) {
        String str = "00时00分00秒";
        if (second == null || second < 0) {
            return str;
        }

        // 得到小时
        long h = second / 3600;
        str = h > 0 ? ((h < 10 ? ("0" + h) : h) + "时") : "00时";

        // 得到分钟
        long m = (second % 3600) / 60;
        str += (m < 10 ? ("0" + m) : m) + "分";

        //得到剩余秒
        long s = second % 60;
        str += (s < 10 ? ("0" + s) : s)+"秒";
        return str;
    }

%>




